package com.sc.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.springboot.pojo.Comment;
import com.sc.springboot.pojo.FavorRecord;
import com.sc.springboot.service.ICommentService;
import com.sc.springboot.service.IUsersService;
import com.sc.springboot.util.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author romeo
 * @since 2025-09-20 03:02:20
 */
@RestController
@RequestMapping("/comment")
//评论
public class CommentController {
    @Autowired
    ICommentService commentService;

    @Autowired
    FavorRecordController favorRecordController;




    //添加评论或者回复
    @RequestMapping("/addReply")
    public ResponseMsg addReply(@RequestBody Comment comment){
        comment.setCommentTime(new Date()); //设置评论时间
        comment.setLikes(0); //设置点赞数为0
        if (commentService.save( comment)){
            //增加偏向记录
            favorRecordController.add(comment.getUserId(), comment.getPostId(),2);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"回复成功");
        }else {
            return ResponseMsg.getInstance(ResponseMsg.FAIL,"回复失败");
        }
    }

    //点赞
    @RequestMapping("/likes")
    public ResponseMsg likes(@RequestParam Integer commentId){
        Comment comment=commentService.getById(commentId); //获取评论
        comment.setLikes(comment.getLikes()+1); //点赞数加1
        commentService.updateById(comment); //更新数据库
        //增加偏向记录
        favorRecordController.add(comment.getUserId(), comment.getPostId(),1);
        return ResponseMsg.success();
    }



}
